<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '../../../static/js/formUtil.js';
	import { errorModal,ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				initValue:{
					fileList:[]
				},
				tworkUserPostCacheKey:"fin-js-hourWage-title2-tworkUserPost-cache",
				bearInstIdCacheKey:"fin-js-hourWage-title2-bearInst-cache",
				partakeUserCache:"fin-js-hourWage-title2-partakeUserCache",
				groupCacheKey:"fin-js-hourWage-title2-group-cache",
				isEdit:"",
				workGroup:"",
				instId:"",
				names:[],
				objArr:[]
			})
		},
		methods:{
			clearUser(){
				let _that = this
				_that.$refs.autoform.clear("title2","workUserId")
				_that.$refs.autoform.clear("title2","workPostId")
				_that.$refs.autoform.clear("title2","instGroupId")
			},
			verifyPartakeUser(id){
				let _that =  this
				_that.om.request(
					"hourWage/verifyPartakeUser",
					{
						userId:id
					}
				).then(res=>{
					if(res.value){
						errorModal(_that,'提示信息',res.value,()=>{
							_that.$refs.autoform.clear("title2","workUserId")
							_that.$refs.autoform.clear("title2","workPostId")
							_that.$refs.autoform.clear("title2","instGroupId")
						})
					}
				})
			},
			initLoad(){
				let _that = this
				let arr = [
					_that.om.request(
						'dictSource/loadCombo',
						{
							sourceId:'289',
							categoryId:'8657'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title2","jobTypeId","data",configSelectData(res[0],'id',"text"))
					_that.$refs.autoform.initSelectIndex("title2","jobTypeId")
				})
			},
			onInputOverOrder(value){
				 let _that = this
				 let str = value.replace(/[\u4E00-\u9FA5\uF900-\uFA2D]/g,'');
				 if(value != str){
					 _that.$refs.autoform.formdata.overOrderCodes = str
				 }
			},
			verifyVirtualGroup(){
				let _that = this
				if(!_that.$refs.autoform.formdata.bearInstId){
					_that.om.toast("请先选择冲减部门",3000)
					return false
				}else{
					return true
				}
			},
			verifyPartakeUserName(){
				let _that  = this
				if(!_that.$refs.autoform.formdata.workUserPostId){
					_that.om.toast("请先选择姓名",3000)
					return false
				}else{
					return true
				}
			},
			getUserList(id,group){
				let _that = this
				_that.$refs.autoform.changeEditor('title2', 'partakeUserIdStr', {
					linkParam: {
						other:JSON.stringify({
							instId:id,
							group:group
						}),
						namefield: "username",
						searchfield: "q",
						initload:true,
						url: "orgInstPosUserOfficial/findUserPagerByInst",
						cachename: _that.partakeUserCache,
						pagination: true
					}
				});
			},
			onSelectAsTeamHour(item){
				let _that = this
				if(item.code === "36925"){
					_that.$refs.autoform.clear('title2','partakeUserIdStr')
					_that.$refs.autoform.changeEditor("title2","partakeUserIdStr",{
						initSelect:[]
					})
				}
			},
			getPosGroups(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title2","bearGroupId",{
					link: '../../public/search/search',
					"linkParam":{
						other:JSON.stringify({
							instId:id,
						}),
						namefield: "groupName",
						searchfield: "q",
						initload:true,
						url:"tinstitutionGroup/findGroupList",
						cachename: _that.groupCacheKey,
						pagination: true
					}
				})
			},
			getBooleanList(edit){
				let _that = this
				_that.om.request(
					"dictZero/findBooleanList",
					{
						onlyRight:true
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title2","asTeamHourId","data",configSelectData(res,"id","dicname"))
					if(edit === 'true'){
						_that.$refs.autoform.initSelectIndex("title2","asTeamHourId")
					}
				})
			},
			onInpustApplyHours(val){
				let _that = this 
				if(_that.$refs.autoform.formdata.hourPrice){
					let applyHours = + val
					let hourPrice = + _that.$refs.autoform.formdata.hourPrice
					_that.$refs.autoform.formdata.totalPrice =  applyHours*hourPrice
				}
			},
			onInputHourPrice(val){
				let _that = this 
				if(_that.$refs.autoform.formdata.applyHours){
					let hourPrice = + val
					let applyHours = + _that.$refs.autoform.formdata.applyHours
					_that.$refs.autoform.formdata.totalPrice =  applyHours*hourPrice
				}
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.groupCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.bearGroupId = d.id
							formdata.bearGroupName = d.groupName
							uni.removeStorage({
								key:_that.groupCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.tworkUserPostCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							_that.om.request("orgInstPosUserOfficial/getPostList", {
								'user.id': d.id,
								'inst.id': _that.instId,
								getInstGroupFlag:true,
								unpackedFlag: true
							}).then(res => {
								if (res.succeed) {
									formdata.workUserId = d.id
									formdata.workUserName = d.username
									if(res.value.length==1){
										formdata.workPostId = res.value[0].id
										formdata.workPostName = res.value[0].posname
										formdata.instGroupId = res.value[0].instGroupId
										formdata.instGroupName = res.value[0].instGroupName
									}else{
										_that.$refs.autoform.clear("title2","workPostId")
										_that.$refs.autoform.clear("title2","instGroupId")
									}
									_that.$refs.autoform.changeEditor("title2","workPostId","data",configSelectData(res.value,'id','posname'))
									_that.$refs.autoform.changeEditor("title2","workPostId","type","selectbox")
									_that.$refs.autoform.initSelectIndex("title2","workPostId")
								} else {
									_that.$refs.autoform.changeEditor("title2","workPostId","type","labelbox")
									_that.om.toast(res.mag)
								}
							})
							
							_that.workGroup = d.virtualGroup
							formdata.workDate = moment().format('YYYY-MM-DD')
							_that.getUserList(_that.instId,_that.workGroup)
							formdata.partakeUserIdStr = ""
							formdata.partakeUserNameStr = ""
							uni.removeStorage({
								key:_that.tworkUserPostCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.bearInstIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.bearInstId = d.id
							formdata.bearInstName = d.instName
							_that.getPosGroups(d.id)
							uni.removeStorage({
								key:_that.bearInstIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.partakeUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.partakeUserIdStr = d.map(e => e.id).join(",")
							formdata.partakeUserNameStr = d.map(e => e.username).join(",")
							_that.$refs.autoform.clear("title2","asTeamHourId")
							_that.$refs.autoform.changeEditor('title2', 'partakeUserIdStr',{
								initSelect:d
							})
							uni.removeStorage({
								key:_that.partakeUserCache
							})
						}
					}
				})
			}	
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							_that.isEdit = e.edit
							_that.instId = e.instId
							changeEditor("title2","workUserId",{
								"link": '/pages/public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										instId:e.instId
									}),
									namefield: 'username',
									searchfield: "q",
									initload:true,
									url:"orgInstPosUserOfficial/findUserPagerByInst",
									cachename: _that.tworkUserPostCacheKey,
									placeholder: urlUtil.userPageUrl().prompt,
									pagination: true
								}
							},null,_that.autoformdata)
							changeEditor('title2', 'bearInstId', {
								"link": "../../public/search/search",
								"linkParam": {
									other: JSON.stringify({
										containBoard: true
									}),
									method: "post",
									namefield: 'instName',
									searchfield: "instName",
									initload: true,
									url: 'tinstitution/findNotBoard1',
									cachename: _that.bearInstIdCacheKey,
									placeholder: "请输入部门名称"
								}
							}, null, _that.autoformdata)
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "34")
							if(_that.formdata.workUserId){
								changeEditor("title2","workPostId","type","selectbox",_that.autoformdata)
								changeEditor("title2","workPostId","data",[],_that.autoformdata)
							}else{
								changeEditor("title2","workPostId","type","labelbox")
							}
							if(_that.formdata.fileList) {
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}
							_that.getBooleanList(_that.isEdit)
							_that.initLoad()
							if(_that.isEdit === "true"){
								let ids = _that.formdata.partakeUserIdStr.split(",")
								_that.om.request(
									"orgInstPosUserOfficial/findTreeByInst",
									{
										instId:e.instId,
										group:_that.workGroup
									}
								).then(result=>{
										if(result.length){
											for(let index in result){
												if(ids.includes(result[index].id+'')){
												  _that.names.push(result[index].text)
												}
											}
											for(let index in ids){
												let obj ={
														id:Number(ids[index]),
														text:_that.names[index]
													}
											 _that.objArr.push(obj)
											}
											if(_that.objArr.length === 1 && _that.objArr[0].id && _that.objArr[0].text){
												_that.$refs.autoform.changeEditor("title2","partakeUserIdStr",{
													initSelect:_that.objArr
												})
											}
											
										}
									})
								}
								_that.$nextTick(function(){
									_that.$refs.autoform.formdata.virtualGroup = _that.formdata.virtualGroup
									_that.$refs.autoform.formdata.virtualGroupName = _that.formdata.virtualGroup
									if(_that.formdata.workUserId){
										_that.om.request("orgInstPosUserOfficial/getPostList", {
											'user.id': _that.formdata.workUserId,
											'inst.id': _that.instId,
											getInstGroupFlag:true,
										}).then(res => {
											_that.$refs.autoform.changeEditor("title2","workPostId","data",configSelectData(res,'id','posname'))
											_that.$refs.autoform.initSelectIndex("title2","workPostId")
										})
									}
									if(_that.$refs.autoform.formdata.bearInstId){
										_that.getPosGroups(_that.$refs.autoform.formdata.bearInstId)
									}
									_that.getUserList(_that.instId,_that.formdata.workGroup)
								})
							}
						}
					})
				}
							
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                  
